﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;

namespace InteractiveMapApp
{
    /// <summary>
    /// instantiates a plane, which will appear on the map for a while
    /// </summary>
    class Plane : AnimationObject
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="width">width of the screen</param>
        /// <param name="height">height of the screen</param>
        public Plane(double width, double height)
        {
            AnimationStoryboard = new Storyboard();
            matrixTransform = new MatrixTransform();

            mainWidth = width;
            mainHeight = height;

            this.HorizontalAlignment = HorizontalAlignment.Left;
            this.VerticalAlignment = VerticalAlignment.Top;
            this.Margin = new Thickness(0, 0, 0, 0);

         
            NameScope.SetNameScope(this, new NameScope());
            NameScope.GetNameScope(this).RegisterName("MatrixTransform", matrixTransform);


            this.RenderTransform = matrixTransform;

           
            //set the image
            this.Source = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/Projector/plane.png", UriKind.Relative));
            BeginAnimation();
        }

        public void BeginAnimation()
        {
            random = new Random();

            double sizeModifier = random.NextDouble() / 1.2;
            if (sizeModifier < 0.5)
            {
                sizeModifier += 0.2;
                if (sizeModifier < 0.5)
                {
                    sizeModifier += 0.2;
                }
            }
            this.MaxHeight = 180 * sizeModifier;
            this.MaxWidth = 200 * sizeModifier;

            // Create the animation path.
            double startY = random.Next(0, (int)mainHeight - 350);
            double pathAngleFactor = random.Next(90, 100);
            PathGeometry animationPath = new PathGeometry();
            PathFigure pFigure = new PathFigure();
            QuadraticBezierSegment qBezierSegment = new QuadraticBezierSegment();
            pathAngleFactor = 1 / pathAngleFactor;
            double randomChoose = random.NextDouble();
            double w = mainWidth;


            if (random.NextDouble() >= 0.5)
            {
                pFigure.StartPoint = new Point(-100, startY);

                if (randomChoose <= 0.25)
                {
                    qBezierSegment.Point1 = new Point(w / 2, (100 * pathAngleFactor * 400) + (startY));
                    qBezierSegment.Point2 = new Point(w + 200, (100 * pathAngleFactor * 100) + (startY));
                }
                else if (randomChoose > 0.25 && randomChoose <= 0.5)
                {
                    qBezierSegment.Point1 = new Point(w / 3, (100 * pathAngleFactor * 600) - (startY));
                    qBezierSegment.Point2 = new Point(w + 200, (100 * pathAngleFactor * 500) - (startY));
                }
                else if (randomChoose > 0.5 && randomChoose <= 0.75)
                {
                    qBezierSegment.Point1 = new Point(w / 8, (100 * pathAngleFactor * 100) + (startY));
                    qBezierSegment.Point2 = new Point(w + 200, (100 * pathAngleFactor * 600) + (startY));
                }
                else
                {
                    ///path 1
                    qBezierSegment.Point1 = new Point(w / 1.2, (100 * pathAngleFactor * 300) + (startY));
                    qBezierSegment.Point2 = new Point(w + 200, (100 * pathAngleFactor * 100) + (startY));
                }
            }
            else
            {
                this.Source = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/Projector/plane_alt.png", UriKind.Relative));

                pFigure.StartPoint = new Point(w + 100, startY);

                if (randomChoose <= 0.25)
                {
                    qBezierSegment.Point1 = new Point(w / 1.2, (100 * pathAngleFactor * 400) + (startY));
                    qBezierSegment.Point2 = new Point(-200, (100 * pathAngleFactor * 100) + (startY));
                }
                else if (randomChoose > 0.25 && randomChoose <= 0.5)
                {
                    qBezierSegment.Point1 = new Point(w / 2, (100 * pathAngleFactor * 600) - (startY));
                    qBezierSegment.Point2 = new Point(-200, (100 * pathAngleFactor * 500) - (startY));
                }
                else if (randomChoose > 0.5 && randomChoose <= 0.75)
                {
                    qBezierSegment.Point1 = new Point(w / 1.33, (100 * pathAngleFactor * 100) + (startY));
                    qBezierSegment.Point2 = new Point(-200, (100 * pathAngleFactor * 600) + (startY));
                }
                else
                {
                    ///path 1
                    qBezierSegment.Point1 = new Point(w / 8, (100 * pathAngleFactor * 300) + (startY));
                    qBezierSegment.Point2 = new Point(-200, (100 * pathAngleFactor * 100) + (startY));
                }
            }

            pFigure.Segments.Add(qBezierSegment);
            animationPath.Figures.Add(pFigure);

            // Freeze the PathGeometry for performance benefits.
            animationPath.Freeze();

            // Create a MatrixAnimationUsingPath to move the
            // button along the path by animating
            // its MatrixTransform.
            MatrixAnimationUsingPath matrixAnimation =
                new MatrixAnimationUsingPath();
            matrixAnimation.PathGeometry = animationPath;
            matrixAnimation.Duration = TimeSpan.FromSeconds(random.Next(60, 90));


            // Set the animation's DoesRotateWithTangent property
            // to true so that rotates the rectangle in addition
            // to moving it.
            matrixAnimation.DoesRotateWithTangent = true;


            // Set the animation to target the Matrix property
            // of the MatrixTransform named "MatrixTransform".
            Storyboard.SetTargetName(matrixAnimation, "MatrixTransform");
            Storyboard.SetTargetProperty(matrixAnimation,
                new PropertyPath(MatrixTransform.MatrixProperty));

            AnimationStoryboard.Children.Clear();
            AnimationStoryboard.Children.Add(matrixAnimation);
            AnimationStoryboard.Completed += new EventHandler(AnimationStoryboard_Completed);

            //matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, matrixAnimation);
            AnimationStoryboard.Begin(this);
        }

        void AnimationStoryboard_Completed(object sender, EventArgs e)
        {
            if (this.Source != null)
            {
                this.Source = null;
            }
            NameScope.GetNameScope(this).UnregisterName("MatrixTransform");
        }

    }
}